package service

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto/rpc"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func PublishVideo(c *gin.Context) {
	var form request.PublishVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.PublishVideo(c, &__.PublishVideoReq{
		UserId: int64(form.UserId),
		Title:  form.Title,
		Image:  form.Image,
		Refer:  form.Refer,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "发布视频失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "发布视频成功", "data": gin.H{
		"videoId": video.VideoId,
	}})
	return
}
func CheckVideo(c *gin.Context) {
	var form request.CheckVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.CheckVideo(c, &__.CheckVideoReq{
		UserId:  int64(form.UserId),
		VideoId: int64(form.VideoId),
		Status:  int64(form.Status),
		Reason:  form.Reason,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "审核视频失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "审核视频成功", "data": gin.H{
		"checkVideoId": video.CheckVideoId,
	}})
	return
}

func GetEpisodeList(c *gin.Context) {
	var form request.GetEpisodeList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	episode, err := config.VideoRpcClient.GetEpisodeList(c, &__.GetEpisodeListReq{
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "获取剧集失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取剧集成功", "data": gin.H{
		"list":        episode.List,
		"episodeList": episode.EpisodeList,
	}})
	return
}

func GetVideoListById(c *gin.Context) {
	var form request.GetVideoListById
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	video, err := config.VideoRpcClient.GetVideoListById(c, &__.GetVideoListByIdReq{
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "获取视频详情失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取视频详情成功", "data": gin.H{
		"Id":           video.Id,
		"Title":        video.Title,
		"Image":        video.Image,
		"Refer":        video.Refer,
		"Status":       video.Status,
		"AreaName":     video.AreaName,
		"CategoryName": video.CategoryName,
		"ChannelName":  video.ChannelName,
	}})
	return
}

func GetVideoList(c *gin.Context) {
	video, err := config.VideoRpcClient.GetVideoList(c, &__.GetVideoListReq{})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "获取剧集失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取剧集成功", "data": gin.H{
		"list": video.List,
	}})
	return
}
